capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

local output "";
local crime "";

/* 
Sibling prep 3
*/


local exp "prworasib";
local range "1997(3)1997" ; /* "1997(3)2000" */
local firstyr 1997 ;	/* Kids turn 18 in 1996 */
local lastyr 2019 ;		/* Last year of crime data */
local lastearnyr 2019 ;

/* ******************* */
/* **** DATA PREP **** */
/* ******************* */

local first 1996 ;
local last 2004 ;
local per_range "20" ; /*18(2)20*/
local thresh 0.8 ; 	/* how many years in the range have to have data for the state/cnty to be included */
include "`do'\crime_statecntyselect.do" ;

use "`crime'\prep2\crime_prworasib_remerge.dta", clear;

/* Diagnosis (only ever used as factor variable, so replace missing values with 999) */
tab n_diag1cat, m;
gen byte n_diag1_infec=(n_diag1cat==1);
gen byte n_diag1_neo=(n_diag1cat==2);
gen byte n_diag1_endo=(n_diag1cat==3);
gen byte n_diag1_blood=(n_diag1cat==4);
gen byte n_diag1_mental=(n_diag1cat==5);
gen byte n_diag1_nerv=(n_diag1cat==6);
gen byte n_diag1_sense=(n_diag1cat==7);
gen byte n_diag1_circ=(n_diag1cat==8);
gen byte n_diag1_resp=(n_diag1cat==9);
gen byte n_diag1_dig=(n_diag1cat==10);
gen byte n_diag1_gu=(n_diag1cat==11);
gen byte n_diag1_preg=(n_diag1cat==12);
gen byte n_diag1_skin=(n_diag1cat==13);
gen byte n_diag1_musc=(n_diag1cat==14);
gen byte n_diag1_cong=(n_diag1cat==15);
gen byte n_diag1_nat=(n_diag1cat==16);
gen byte n_diag1_ill=(n_diag1cat==17);
gen byte n_diag1_inj=(n_diag1cat==18);
gen byte n_diag1_none=(n_diag1cat==.);
replace n_diag1cat = 999 if n_diag1cat == . ;

/* Family structure */
tab toa, m;
gen byte n_fam_singmom=	(toa=="DM" | toa=="BM");
gen byte n_fam_nopar=	(toa=="DC" | toa=="BC");
tab n_fam_singmom, m;
tab n_fam_nopar, m;

/* Male */
tab n_male, m;

/* Severity */
tab dibmdr, m;
gen byte n_dibmdr_miss=(dibmdr=="");
gen byte n_dibmdr_mip=(dibmdr=="3");
gen byte n_dibmdr_mine=(dibmdr=="5" | dibmdr=="7");
gen byte n_dibmdr_mie=(!n_dibmdr_miss & !n_dibmdr_mip & !n_dibmdr_mine);
tab n_dibmdr_mie, m;
tab n_dibmdr_mip, m;
tab n_dibmdr_mine, m;

/* Age at entry (only ever used as factor variable, so replace missing values with 999) */
tab n_firstage, m;
replace n_firstage = 999 if n_firstage == . ;
tab n_firstage, m ;

/* First stage variables: on SSI as kid, unfavorable age 18 */
gen byte n_age18unfav=(age18_final_con==0);
summ n_age18unfav ;


/* Sibling sex */
gen sib_male = . ;
replace sib_male = 1 if (sib_sex == "M") ;
replace sib_male = 0 if (sib_sex == "F") ;



/* ************************** */
/* **** SAMPLE SELECTION **** */
/* ************************** */

/* Create running variable */
drop dobdd_stata dobdd_run ;
gen dobdd_stata	=mdy(month(age18_dob),day(age18_dob),year(age18_dob)+18); 
gen dobdd_run	=dobdd_stata-mdy(08,22,1996);
gen dobwk_stata	=wofd(mdy(month(age18_dob),day(age18_dob),year(age18_dob)+18));
gen dobwk_run	=dobwk_stata-wofd(mdy(08,22,1996));
gen dobmo_stata	=ym(year(age18_dob)+18,month(age18_dob)) ;
gen dobmo_run	=dobmo_stata-mofd(mdy(08,22,1996)) ;

count ;
keep ifyear(first_pay_dte)<1996 ;
count;


/* ********************************************************************** */
/* **** CREATE CRIME VARIABLES USING DIFFERENT GEO/YEAR DENOMINATORS **** */
/* ********************************************************************** */

/* Determine county-years that make up denominator */
rename pdscc pdscc_full ;
gen pdscc = substr(pdscc_full,1,5) ;
gen temp_st = substr(pdscc_full,1,2) ;
tab temp_st, m ;
drop temp_st pdscc_full ;

merge m:1 pdscc using `cnty_year', keepusing(keep_*) ;
drop if _merge == 2 ;
drop _merge ;
foreach var of varlist keep* { ;
	replace `var' = 0 if `var' == . ;
} ;

summ keep* ;


/* ******************************** */
/* **** DEFINE CRIME VARIABLES **** */
/* ******************************** */

local char_cond 	2 ;
local drug_cond 	3 ;
local traf_cond     4 ;
local fel_cond		5 ;
local genr_cond		6 ;
local nogn_cond  	7 ;
local prop_cond 	8 ;
local pord_cond     9 ;
local viol_cond 	10 ;
local wknd_cond		11 ;
local fcvn_cond		12 ;
local acvn_cond     13 ;
local inca_cond 	14 ;
local par_cond 		15 ;
local prob_cond 	16 ;

local dgsl_cond 1 ;
local dgot_cond 2 ;
local dgps_cond 3 ;
local polq_cond	4 ;
local poot_cond	5 ;
local povc_cond	6 ;
local povl_cond	7 ;
local powp_cond	8 ;
local prbg_cond 9 ;
local prfd_cond 10 ;
local prtf_cond 11 ;
local vrob_cond 12 ;
local vlct_cond 13 ;
local vlpa_cond 14 ;
local vlpr_cond 15 ;
local hinc_cond 16 ;

local vcch_cond 1 ;
local vcgb_cond 2 ;
local vcpd_cond 3 ;
local vcpp_cond 4 ;
local vcps_cond 5 ;

local nias_cond 1 ; 
local nide_cond 2 ; 
local nidc_cond 3 ; 
local nidp_cond 4 ; 
local nidu_cond 5 ; 
local nilq_cond 6 ; 
local niob_cond 7 ; 
local nisx_cond 8 ; 
local nitf_cond 9 ; 
local nitp_cond 10 ; 
local niwp_cond 11 ;	

local any_abbr		"any" ;
local char_abbr 	"2c" ;
local drug_abbr 	"3cd" ;
local traf_abbr 	"4ct" ;
local fel_abbr  	"5cf" ;
local genr_abbr 	"6cg" ;
local nogn_abbr     "7cn" ;
local prop_abbr 	"8cp" ;
local pord_abbr     "9co" ;
local viol_abbr 	"10cv" ;
local wknd_abbr 	"11cw" ;
local fcvn_abbr 	"12fc" ;
local acvn_abbr     "13fm" ;
local inca_abbr		"14ie" ;	
local par_abbr		"15pa" ;
local prob_abbr		"16pr" ;
local ipp_abbr		"ipp" ;
local jail_abbr		"jail" ;

foreach stub in 
	dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc  
	nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp
	vcch vcgb vcpd vcpp vcps
	{ ;
		local `stub'_abbr "``stub'_cond'`stub'" ;
	} ;

local type_list_main "char genr nogn drug viol prop pord fel fcvn acvn inca" ; 
local type_list_bk "dgsl dgot dgps polq poot povc povl powp prbg prfd prtf vrob vlct vlpa vlpr hinc" ;
local type_list_nobk "nias nide nidc nidp nidu nilq niob nisx nitf nitp niwp" ;
local type_list_vice "vcch vcgb vcpd vcpp vcps" ;
local type_list "`type_list_main' `type_list_bk' `type_list_nobk' `type_list_vice' " ;

foreach stub in `type_list_main' { ;
	local `stub'_event "event" ;
} ;
foreach stub in `type_list_bk' { ;
	local `stub'_event "bk_event" ;
} ;
foreach stub in `type_list_nobk' { ;
	local `stub'_event "nobk_event" ;
} ;
foreach stub in `type_list_vice' { ;
	local `stub'_event "vice_event" ;
} ;

/* There aren't as many vice variables as the other ones, so need to fill in */
forval i = 13(1)20 { ;
	capture gen vice_event`i' = . ;
	capture gen vice_event_date`i' = . ;
} ;

rename indiv_match temp1 ;
gen byte temp2 = (event_date1 != .) ;

forval i = 1(1)2 { ;
		gen n_crime`i'any_post = temp`i' if 	keep_`firstyr'post_any == 1 ;
} ;


forval i=1(1)20 { ;
	tostring(event`i'), replace format(%18.0g) ;
	forval j = 3(1)9 { ;
		replace event`i' = subinstr(event`i',"`j'","2",.) ;
	} ;
} ;

foreach class in bk nobk vice { ;
	forval i=1(1)20 { ;
		recast double `class'_event`i' ;
		tostring(`class'_event`i'), replace format(%19.0g) ;
		forval j = 3(1)9 { ;
			replace `class'_event`i' = subinstr(`class'_event`i',"`j'","2",.) ;
		} ;
	} ;
} ;

forval yr = 1997(1)2019 { ;
	gen days_in_ipp`yr' = days_in_prison`yr' + days_in_parole`yr' + days_in_probation`yr' ;
} ;

/* **** GENERIC **** */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
	
		/* Indicator */
		gen 		n_crime2any_`start'_`end'= 0 
				if (keep_`start'_`end'_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2any_`start'_`end' = 1 
				if (keep_`start'_`end'_any == 1)
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' ;
			} ;
			
		forval yr=`start'(1)`end' { ;
			replace n_crime2any_`start'_`end' = 1 
				if n_crime2any_`start'_`end' != . 
				& days_in_ipp`yr' > 0 & days_in_ipp`yr' != . ;
		} ;
		
		/* Total number */
		gen 		n_crime2anyt_`start'_`end'= 0 
				if (keep_`start'_`end'_any == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2anyt_`start'_`end' = n_crime2anyt_`start'_`end' + 1 
				if (keep_`start'_`end'_any == 1)
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2any`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2any`yr'_`start'_`end' = 1
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2anyt`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_any == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2anyt`yr'_`start'_`end' = c_crime2anyt`yr'_`start'_`end' + 1 
					if (keep_`start'_`end'_any == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' ;
				} ;
			} ;
				
	} ;	/* end end */
} ;	/* end start */


/* **** SPECIFIC TYPES OF CRIME **** */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			
			/* Indicator */
			gen 		n_crime2`type'_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type'_`start'_`end' = 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `end' 
					& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			/* Total number */
			gen 		n_crime2`type't_`start'_`end'= 0 
					if (keep_`start'_`end'_``type'_abbr' == 1) ;
			forval i=1(1)20 { ;
				replace n_crime2`type't_`start'_`end' = n_crime2`type't_`start'_`end' + 1 
					if (keep_`start'_`end'_``type'_abbr' == 1)
					& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `end' 
					& substr(``type'_event'`i',``type'_cond',1) == "2" ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */


forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2vnrb_`start'_`end'= 0 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrb_`start'_`end' = 1 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
				& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `end' 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
		/* Total number */
		gen 		n_crime2vnrbt_`start'_`end'= 0 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1) ;
		forval i=1(1)20 { ;
			replace n_crime2vnrbt_`start'_`end' = n_crime2vnrbt_`start'_`end' + 1 
				if (keep_`start'_`end'_`viol_abbr' == 1 & keep_`start'_`end'_`vrob_abbr' == 1)
				& year(`viol_event'_date`i' ) >= `start' & year(`viol_event'_date`i' ) <= `end' 
				& substr(`viol_event'`i',`viol_cond',1) == "2" & substr(`vrob_event'`i',`vrob_cond',1) != "2" ;
			} ;
			
		} ;	/* end end */
	} ;	/* end start */

/* cumulative */
foreach type in `type_list' { ;
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
			
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type'`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type'`yr'_`start'_`end' = 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `yr'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			forval yr = `=`end'-(4*4)'(4)`end' { ;
				gen 		c_crime2`type't`yr'_`start'_`end'= 0 
						if (keep_`start'_`end'_``type'_abbr' == 1) ;
				forval i=1(1)20 { ;
					replace c_crime2`type't`yr'_`start'_`end' = c_crime2`type't`yr'_`start'_`end' + 1 
						if (keep_`start'_`end'_``type'_abbr' == 1)
						& year(``type'_event'_date`i' ) >= `start' & year(``type'_event'_date`i' ) <= `yr'
						& substr(``type'_event'`i',``type'_cond',1) == "2" ;
					} ;
				} ;
				
			} ;	/* end end */
		} ;	/* end start */
} ; /* type */

/* Incarceration, parole, or probation FLOW */
forval start = `range' { ;
	foreach end in `list_`start'' { ;
		
		/* Indicator */
		gen 		n_crime2ipp_`start'_`end'= 0 
				if (keep_`start'_`end'_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ipp_`start'_`end' = 1 
				if (keep_`start'_`end'_ipp == 1 )
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
			
		/* Total number */
		gen 		n_crime2ippt_`start'_`end'= 0 
				if (keep_`start'_`end'_ipp == 1 ) ;
		forval i=1(1)20 { ;
			replace n_crime2ippt_`start'_`end' = n_crime2ippt_`start'_`end' + 1 
				if (keep_`start'_`end'_ipp == 1 )
				& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `end' 
				& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
			} ;
			
		/* Cumulative */
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ipp`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ipp`yr'_`start'_`end' = 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
			
		forval yr = `=`end'-(4*4)'(4)`end' { ;
			gen 		c_crime2ippt`yr'_`start'_`end'= 0 
					if (keep_`start'_`end'_ipp == 1) ;
			forval i=1(1)20 { ;
				replace c_crime2ippt`yr'_`start'_`end' = c_crime2ippt`yr'_`start'_`end' + 1 
					if (keep_`start'_`end'_ipp == 1)
					& year(event_date`i' ) >= `start' & year(event_date`i' ) <= `yr' 
					& (substr(event`i',`inca_cond',1) == "2" | substr(event`i',`par_cond',1) == "2" | substr(event`i',`prob_cond',1) == "2") ;
				} ;
			} ;
		
		} ;	/* end end */
} ;	/* end start */


/* Length of IPP spells / indicator for any IPP */
local full_hinc "hprison" ;
local full_inca "prison" ;
local full_par  "parole" ;
local full_prob "probation" ;
local full_ipp 	"ipp" ;
local full_jail "jail" ;

local correct_list "hinc inca par prob ipp jail" ; 


foreach type in `correct_list' { ;
	
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
				egen 		n_crime2s`type'_`start'_`end' = rowmean(days_in_`full_`type''`start'-days_in_`full_`type''`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;	
				
				/* cumulative */
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					egen c_crime2s`type'`yr'_`start'_`end' = rowtotal(days_in_`full_`type''1997-days_in_`full_`type''`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

/* change denominator to that year rather than entire 2000-2016 period */
foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
		gen n_yrcrime2s`type'_`i' = days_in_`full_`type''`i' if (keep_yr`i'_`type' == 1) ;
	} ;
} ;


/* new "any" stock variables */
foreach abbr in `correct_list' { ;
	forval yr = 1997(1)2019 { ;
		gen byte any_`abbr'`yr' = (days_in_`full_`abbr''`yr' > 0) ;
		replace  any_`abbr'`yr' = . if days_in_`full_`abbr''`yr' == . ;
	} ;
} ;

foreach type in `correct_list' { ;
	
	forval start = `range' { ;
		foreach end in `list_`start'' { ;
				egen 		n_crime2a`type'_`start'_`end' = rowmean(any_`type'`start'-any_`type'`end') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				
				/* cumulative */
				forval yr = `=`end'-(4*4)'(4)`end' { ;
					egen c_crime2a`type'`yr'_`start'_`end' = rowtotal(any_`type'1997-any_`type'`yr') if (keep_`start'_`end'_``type'_abbr' == 1 ) ;
				} ;
				
			} ;	/* end end */
	} ;	/* end start */

} ; /* end type */

foreach type in `correct_list' { ;
	forval i=`firstyr'(1)`lastyr' { ;
		gen n_yrcrime2a`type'_`i' = any_`type'`i' if (keep_yr`i'_`type' == 1) ;
	} ;
} ;

summ n_crime* c_crime* n_yr* ;

gen byte n_post=(dobdd_run>0);
tab n_post, m;

rename keep_1997_2017_any tkeep_1997_2017_any ;
drop 
	first_name last_name idu middle_name days_in_* event* day_of_week* keep_* ;
rename tkeep_1997_2017_any keep_1997_2017_any ;

save "`crime'\prep2\crime_prworasib_prepforreg.dta", replace ;

capture log close;
